package PAPForLargeOntologiesToText;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class ConvertAlignHumanMouse {
	public static void main(String[] args) {
		ArrayList<String> alignReferece = ReaderRDF.alignmentForHumanMouseOntology("src/ontologies/reference_human_mouse.rdf");
		ArrayList<String> alignRefereceText = ReaderRDF.alignmentReadyForOntology("src/ontologies/reference_human_mouse_string.txt");
		ArrayList<String> newAlignReferece = new ArrayList<String>();
		
		
		try {
			OWLOntology humanOnto = readOWL("src/ontologies/human.owl");
			OWLOntology mouseOnto = readOWL("src/ontologies/mouse.owl");
			ArrayList<OWLClass> human = new ArrayList<OWLClass>(humanOnto.getClassesInSignature());
			ArrayList<OWLClass> mouse = new ArrayList<OWLClass>(mouseOnto.getClassesInSignature());
			
			HashMap<String, String> mapHuman = new HashMap<String, String>();
			for (OWLClass owlClass : human) {
				mapHuman.put(owlClass.getIRI().getFragment(), ReaderRDF.getLabel(owlClass, humanOnto, false));
			}
			
			HashMap<String, String> mapMouse = new HashMap<String, String>();
			for (OWLClass owlClass : mouse) {
				mapMouse.put(owlClass.getIRI().getFragment(), ReaderRDF.getLabel(owlClass, mouseOnto, false));
			}
			
			for (String pair : alignReferece) {
				String conceptHuman = pair.split(" - ")[0];
				String conceptMouse = pair.split(" - ")[1];
				
				String newPair = mapHuman.get(conceptHuman) + " - " + mapMouse.get(conceptMouse);
				newAlignReferece.add(newPair);
			}
			
			OutputStream os = new FileOutputStream("src/ontologies/reference_human_mouse_string.txt");
			for (String string : newAlignReferece) {
				String line = string + "\n";
				os.write(line.getBytes());
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Method to read owl file.
	 * @param path
	 * @return OWL Ontology
	 * @throws OWLOntologyCreationException
	 */
	private static OWLOntology readOWL(String path) throws OWLOntologyCreationException {
		String DOCUMENT_IRI = path;
		File file = new File(DOCUMENT_IRI);
		// Create our ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		// Load a copy of the people+pets ontology. We'll load the ontology from
		// the web (it's acutally located in the TONES ontology repository).
		IRI docIRI = IRI.create(file);
		// We load the ontology from a document - our IRI points to it directly
		OWLOntology ontology = manager.loadOntologyFromOntologyDocument(docIRI);
		
		return ontology;
	}
	
}
